﻿using System;
using System.Net;
using System.Threading;

using UdpTransceiver;
using ParametersParser;
using Communicator;


namespace RepeaterService
{
    class Program
    {
        
        static void Main(string[] args)
        {
            Thread.GetDomain().UnhandledException += new UnhandledExceptionEventHandler(Program_UnhandledException);
            
            Communicator.Communicator Communicator = new Communicator.Communicator();

            Console.WriteLine("Start listening? (y/n)");
            if (Console.ReadLine() == "y")
            {
                Console.WriteLine("Initializing communicator...( Q - exit )");
 
                Communicator.Start();

                Communicator.AddParameter("COURSE", ParameterDirection.output);
                Communicator.AddParameter("BEACON1", ParameterDirection.output);
                Communicator.AddParameter("BEACON2", ParameterDirection.output);
                Communicator.AddParameter("AIR_ALTITUDE", ParameterDirection.output);
                //Communicator.AddParameter("TotReset", ParameterDirection.output);
            }

            string r = "0";
            
            foreach (Parameter Item in Communicator.Parameters)
                Communicator.SetParameterValueByName(Item.Name, "0");

            Thread.Sleep(1000);

            int i = 0;
            int a = 0;
            while ( true )
            {
                Console.Clear();
                 foreach (Parameter Item in Communicator.Parameters)
                    Console.Write("{0} = {1};", Item.Name, Item.Value );
 
                     //i = i + 1;
 
                Communicator.SetParameterValueByName("COURSE", i.ToString());
                Communicator.SetParameterValueByName("BEACON1", i.ToString());
                Communicator.SetParameterValueByName("BEACON2", i.ToString());
                Communicator.SetParameterValueByName("AIR_ALTITUDE", i.ToString());
                //Communicator.SetParameterValueByName("TotReset", r);
                r = "0";
                Thread.Sleep(20);
                ConsoleKeyInfo cki = new ConsoleKeyInfo();
                // fly stopping using Q key
                if (Console.KeyAvailable == true)
                {
                    cki = Console.ReadKey(true);
                    if (cki.Key == ConsoleKey.Q)
                    {
                        Communicator.Stop();
                        break;
                    }
                    switch (cki.Key)
                    {
                        case ConsoleKey.W:
                            i+=2;
                            break;
                        case ConsoleKey.Z:
                            i-=2;
                            break;
                        case ConsoleKey.R:
                            r = "1";
                            break;
                    }
                }
            }
        }

        static void Program_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Console.WriteLine(((Exception)e.ExceptionObject).Message );
        }
           
    }
}
